/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package maincz.tests.webapp.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * Entita pro zaznamy tabulky USERS
 * @author Main
 */
@Entity
@Table(name="USERS")
public class User implements Serializable, UserDetails {
    
    @Id
    @Column(name="ID")
    private Long id;
    
    @Column(name="NAME")
    private String name;
    
    @Column(name="LOGIN")
    private String login;
    
    @Column(name="PASSWORD")
    private String password;
    
    @Column(name="EMAIL")
    private String email;
    
    @Column(name="ENABLED")
    private Boolean enabled;
    
    @Column(name="D_CREATED")
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date dCreated;
    
    @Column(name="D_MODIFIED")
    @Temporal(javax.persistence.TemporalType.DATE)
    private Date dModified;
    
    @ManyToMany(cascade= CascadeType.ALL)
    @JoinTable(name="USER_ROLES", 
        joinColumns={@JoinColumn(name="ID_USER")},
        inverseJoinColumns={@JoinColumn(name="ID_ROLE")})
    private Set<Role> roles = new HashSet<Role>();
    
    public Date getDCreated() {
        return dCreated;
    }

    public void setDCreated(Date d_created) {
        this.dCreated = d_created;
    }

    public Date getDModified() {
        return dModified;
    }

    public void setDModified(Date d_modified) {
        this.dModified = d_modified;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Boolean getEnabled() {
        return enabled;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
    
    public String getLogin() {
        return this.login;        
    }
    
    public void setLogin(String login) {
        this.login = login;                
    }

    @Override
    public String getUsername() {
        return login;
    }

    public void setUsername(String username) {
        this.login = username;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Set<Role> getRoles() {
        return roles;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }

    @Override
    public Set<GrantedAuthority> getAuthorities() {
        return (Set) this.roles;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return this.enabled;
    }
        
}
